home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / System / Window / Window Looks / Next WDEF 7.0 / Source / Draw WDEF.c next >
Encoding:
C/C++ Source or Header  |  1991-06-17  |  14.2 KB  |  616 lines  |  [TEXT/KAHL]

  1. /*
  2.     WDEF Draw.c
  3.     
  4. */
  5.  
  6. #include <MacTypes.h>
  7. #include <Color.h>
  8. #include <FontMgr.h>
  9. #include <MemoryMgr.h>
  10. #include <OSUtil.h>
  11. #include <Quickdraw.h>
  12. #include <ToolboxUtil.h>
  13. #include <WindowMgr.h>
  14.  
  15. typedef struct {
  16.     Rect        stdState, userState;
  17.     Boolean        buttonState;
  18. } WSDRecord, *WSDPointer, **WSDHandle;
  19.  
  20. /* #define testing */
  21.  
  22. #ifdef testing
  23. #define nil 0
  24. #endif testing
  25.  
  26. #define buttonState    (**(WSDHandle)(*(WindowPeek)window).dataHandle).buttonState
  27.  
  28. #define plain                0                /* used to set a text face to plain text */
  29. #define blackHex            0xFFFFFFFF
  30. #define gray1Hex            0x55AA55AA
  31. #define gray2Hex            0xAA55AA55
  32. #define whiteHex            0x00000000
  33. #define goAway                0xFFFF8002
  34.  
  35. /* AAAB9556A00B9556A2BB9556A2BB9556A2BB97F6AAAB9556FFFFAAAA */
  36. #define zoom                0xFFFE80039556AAAB9556AAAB900EA55B92AEA55B92AEB7FB9FFEAAABFFFE5555
  37.  
  38. typedef        short           SICN[17];
  39. typedef        SICN            *SICNPtr;
  40.                             
  41. typedef struct {
  42.     long        black[2], white[2], gray[2];
  43.     RGBColor    cwhite, clight, cgray, cdark, cblack, chilite, ctext;
  44. } patsRec, *patsPtr;
  45.  
  46. RGBColor PackGray(brightness)
  47.     int            brightness;
  48. {
  49.     RGBColor    gray;
  50.     
  51.     gray.red = gray.green = gray.blue = brightness;
  52.     return gray;
  53. }
  54.  
  55. Abs(i)
  56. int i;
  57. {
  58.     if (i < 0)
  59.         return(-i);
  60.     return(i);
  61. }
  62.  
  63. /*————————————————————
  64.             drawing my window
  65. ————————————————————*/
  66. drawMyWindow( var, window, param )
  67.     short        var;        /* variation code on this type window */
  68.     WindowPtr    window;            /* pointer to the window */
  69.     long        param;        /* parameter used for many things */
  70. {
  71.     SysEnvRec    thisWorld;
  72.     OSErr        err;
  73.     GrafPtr        wport, portSave;
  74.     Boolean        hasColorQD;
  75.     
  76.     PenState    savedPen;
  77.     PixPatHandle    pnPatSave, bkPatSave, fillPatSave;
  78.     RGBColor    pnClrSave, bkClrSave;
  79.     RgnHandle    clipSave, tempRgn;
  80.  
  81.     patsRec        patstor;
  82.     patsPtr        pat;
  83.     
  84.     Rect        windowRect;
  85.     GDHandle    gdh;
  86.     CTabHandle    colors;
  87.         
  88.     if ( !(*(WindowPeek)window).visible ) return;
  89.     
  90.     pat = &patstor;
  91.     
  92.     err = SysEnvirons( 1, &thisWorld );        /* assess if we have a color comp */
  93.     if (err != noErr) hasColorQD = false;
  94.     else hasColorQD = thisWorld.hasColorQD;
  95.     
  96.     if (hasColorQD) {
  97.         GetPort(&portSave);
  98.         GetCWMgrPort(&wport);
  99.         SetPort(wport);
  100.         
  101.         pnPatSave = NewPixPat();
  102.         bkPatSave = NewPixPat();
  103.         fillPatSave = NewPixPat();
  104.         
  105.         CopyPixPat(((CGrafPtr)wport)->pnPixPat, pnPatSave);
  106.         CopyPixPat(((CGrafPtr)wport)->bkPixPat, bkPatSave);
  107.         CopyPixPat(((CGrafPtr)wport)->fillPixPat, fillPatSave);
  108.         GetForeColor(&pnClrSave);
  109.         GetBackColor(&bkClrSave);
  110.         
  111.         pat->cblack = PackGray(0);
  112.         pat->cdark = PackGray(0x4000);
  113.         pat->cgray = PackGray(0x8000);
  114.         pat->clight = PackGray(0xc000);
  115.         pat->cwhite = PackGray(0xffff);
  116.         
  117.         if ((window->portBits.rowBytes & (3<<14)) == 0xc000) {
  118.             GetAuxWin(window, &colors);
  119.             pat->chilite = (**(GrafVars**)((CGrafPtr)window)->grafVars)
  120.                 .rgbHiliteColor;
  121.             pat->ctext = (**colors).ctTable[2].rgb;
  122.         } else {
  123.             pat->chilite = pat->cgray;
  124.             pat->ctext = pat->cblack;
  125.         }
  126.     }
  127.     
  128.     clipSave = NewRgn();
  129.     tempRgn = NewRgn();
  130.     GetClip(clipSave);
  131.     windowRect = (**(*(WindowPeek)window).strucRgn).rgnBBox;
  132.         
  133.     if ( Abs(windowRect.left % 2) == Abs(windowRect.top % 2) )
  134.         pat->gray[0] = pat->gray[1] = gray1Hex;
  135.     else
  136.         pat->gray[0] = pat->gray[1] = gray2Hex;
  137.     
  138.     pat->black[0] = pat->black[1] = blackHex;
  139.     pat->white[0] = pat->white[1] = whiteHex;
  140.     GetPenState( &savedPen );
  141.     
  142.     if (hasColorQD) {
  143.         gdh = GetDeviceList();
  144.         do {
  145.             if (TestDeviceAttribute(gdh, 13) && TestDeviceAttribute(gdh, 15)) {
  146.                 HLock(gdh);
  147.                 if (RectInRgn(&(**gdh).gdRect, clipSave)) {
  148.                     RectRgn( tempRgn, &(**gdh).gdRect );
  149.                     SectRgn(clipSave, tempRgn, tempRgn);
  150.                     SetClip(tempRgn);
  151.                     if ((**(**gdh).gdPMap).cmpSize >= 4)
  152.                         drawonce(param, var, window, windowRect,
  153.                             pat, true);
  154.                     else
  155.                         drawonce(param, var, window, windowRect,
  156.                             pat, false);
  157.                 }
  158.                 HUnlock(gdh);
  159.             }
  160.         } while ((gdh = GetNextDevice(gdh)) != nil);
  161.     } else
  162.         drawonce(param, var, window, windowRect, pat, false);
  163.     
  164.     SetClip(clipSave);
  165.     DisposeRgn(clipSave);
  166.     DisposeRgn(tempRgn);
  167.     
  168.     SetPenState(&savedPen);
  169.     if (hasColorQD) {
  170.         RGBForeColor(&pnClrSave);
  171.         RGBBackColor(&bkClrSave);
  172.         
  173.         CopyPixPat(pnPatSave, ((CGrafPtr)wport)->pnPixPat);
  174.         CopyPixPat(bkPatSave, ((CGrafPtr)wport)->bkPixPat);
  175.         CopyPixPat(fillPatSave, ((CGrafPtr)wport)->fillPixPat);
  176.         DisposPixPat(pnPatSave);
  177.         DisposPixPat(bkPatSave);
  178.         DisposPixPat(fillPatSave);
  179.         
  180.         SetPort(portSave);
  181.     }
  182.     
  183. } /* end of drawMyWindow() */
  184.  
  185. DrawPane(pat, thisRect, invert, drawColor)
  186.     patsPtr        pat;
  187.     Rect        thisRect;
  188.     Boolean        invert;
  189.     Boolean        drawColor;
  190. {
  191.     /* fill the area with white */
  192.     if (invert)
  193.         PenPat( &pat->black );
  194.     else
  195.         PenPat( &pat->white );
  196.     PaintRect( &thisRect );
  197.     /* add the bottom shadow */
  198.     PenSize( 1, 1 );
  199.     if (drawColor) {
  200.         PenPat( &pat->black );
  201.         if (invert)
  202.             RGBForeColor(&pat->cdark);
  203.         else
  204.             RGBForeColor(&pat->clight);
  205.     } else {
  206.         if (invert)
  207.             PenPat(&pat->black);
  208.         else
  209.             PenPat(&pat->white);
  210.     }
  211.     MoveTo(thisRect.left+1, thisRect.bottom-1);
  212.     LineTo(thisRect.right-1, thisRect.bottom-1);
  213.     LineTo(thisRect.right-1, thisRect.top+1);
  214.     /* add the top shadow */
  215.     if (drawColor) {
  216.         PenPat( &pat->black );
  217.         if (invert)
  218.             RGBForeColor(&pat->clight);
  219.         else
  220.             RGBForeColor(&pat->cdark);
  221.     } else {
  222.         if (invert)
  223.             PenPat(&pat->white);
  224.         else
  225.             PenPat(&pat->black);
  226.     }
  227.     MoveTo( thisRect.left, thisRect.bottom-1 );
  228.     LineTo( thisRect.left, thisRect.top );
  229.     LineTo( thisRect.right-1, thisRect.top );
  230. }
  231.  
  232. drawWindowTitle( window, titleRect, pat, hilited, drawColor )
  233.     WindowPtr    window;
  234.     Rect        *titleRect;
  235.     patsPtr        pat;
  236.     Boolean        hilited, drawColor;
  237. {
  238.     short        savedFont;
  239.     short        savedSize;
  240.     Style        savedFace;
  241.     Str255        windowTitle;
  242.     short        titleWidth;
  243.     RgnHandle    oldClip, newClip;
  244.     GrafPtr        aPort;
  245.     
  246.     GetPort(&aPort);
  247.     savedFont = aPort->txFont;
  248.     savedSize = aPort->txSize;
  249.     savedFace = aPort->txFace;
  250.  
  251.     TextFont( 3 );                                /* set geneva */
  252.     TextSize( 9 );                                /* and small */
  253.     TextFace( plain );                            /* and plain or italic */
  254.     
  255.     GetWTitle( window, windowTitle );
  256.     
  257.     titleWidth = StringWidth( windowTitle );
  258.     
  259.     if ( titleWidth > 0 ) {
  260.         
  261.         titleWidth += 12;                        /* enough for edges */
  262.     
  263.         if ( ( (*titleRect).right - (*titleRect).left ) > titleWidth )
  264.             (*titleRect).right = (*titleRect).left + titleWidth;
  265.         
  266.         if (hilited) DrawPane(pat, *titleRect, false, drawColor);
  267.         
  268.         /* now shrink the box & set the clip region in case of a long title */
  269.         oldClip = NewRgn();
  270.         newClip = NewRgn();
  271.         GetClip( oldClip );
  272.         
  273.         InsetRect( titleRect, 1, 1 );
  274.         RectRgn( newClip, titleRect );
  275.         SectRgn( oldClip, newClip, newClip );
  276.         SetClip( newClip );
  277.         
  278.         /* now position ourselves & draw the title */
  279.         PenPat(&pat->black);
  280.         if (drawColor) {
  281.             if (hilited)
  282.                 RGBForeColor(&pat->ctext);
  283.             else
  284.                 RGBForeColor(&pat->cwhite);
  285.         }
  286.         
  287.         MoveTo( (*titleRect).left + 5, (*titleRect).bottom - 2 );
  288.         DrawString( windowTitle );
  289.         
  290.         SetClip( oldClip );
  291.         DisposeRgn( oldClip );
  292.         DisposeRgn( newClip );
  293.                 
  294.     } /* enough of a title to go with */
  295.     
  296.     TextFont(savedFont);
  297.     TextSize(savedSize);
  298.     TextFace(savedFace);
  299. }
  300.  
  301. void PlotSICN(Rect *theRect, SICNPtr theSICN) {
  302.     auto    char    state;        /* saves original flags of 'SICN' handle */
  303.     auto    BitMap    srcBits;    /* built up around 'SICN' data so we can _CopyBits */
  304.             GrafPtr    aPort;
  305.  
  306.         GetPort(&aPort);
  307.  
  308.         /* set up the small icon's bitmap */
  309.         srcBits.baseAddr = (Ptr) theSICN;
  310.         srcBits.rowBytes = 2;
  311.         SetRect(&srcBits.bounds, 0, 0, 16, 16);
  312.  
  313.         /* draw the small icon in the current grafport */
  314.         CopyBits(&srcBits,&aPort->portBits,&srcBits.bounds,theRect,srcCopy,nil);
  315.  
  316. }
  317.  
  318.  
  319. ToggleGoaway(frame, pat, hilite, drawColor)
  320. Rect        frame;
  321. patsPtr        pat;
  322. Boolean        hilite;
  323. Boolean        drawColor;
  324. {
  325.     DrawPane(pat, frame, hilite, drawColor);
  326.     InsetRect(&frame, 2, 2);
  327.     PenPat(&pat->black);
  328.     
  329.     if (hilite) {
  330.         if (drawColor)
  331.             RGBForeColor(&pat->chilite);
  332.         else
  333.             PenPat(&pat->white);
  334.     } else {
  335.         if (drawColor)
  336.             RGBForeColor(&pat->cblack);
  337.     }
  338.     PaintOval(&frame);
  339. }
  340.  
  341. ToggleZoom(frame, pat, hilite, drawColor)
  342. Rect        frame;
  343. patsPtr        pat;
  344. Boolean        hilite;
  345. Boolean        drawColor;
  346. {
  347.     DrawPane(pat, frame, hilite, drawColor);
  348.     InsetRect(&frame, 2, 2);
  349.     --frame.right; --frame.bottom;
  350.     PenPat(&pat->black);
  351.     
  352.     if (hilite) {
  353.         if (drawColor)
  354.             RGBForeColor(&pat->chilite);
  355.         else
  356.             PenPat(&pat->white);
  357.     } else {
  358.         if (drawColor)
  359.             RGBForeColor(&pat->cblack);
  360.     }
  361.     PaintRect(&frame);
  362. }
  363.  
  364. DrawFrame(pat, frame, width, drawColor)
  365.     patsPtr    pat;
  366.     Rect    frame;
  367.     int        width;
  368.     Boolean    drawColor;
  369. {
  370.     frame.right -= 2;
  371.     frame.bottom -= 2;
  372.     
  373.     PenSize(width, width);
  374.     if (drawColor)
  375.         RGBForeColor(&pat->clight);
  376.     else
  377.         PenPat( &pat->white );
  378.     FrameRect(&frame);
  379.     
  380.     OffsetRect(&frame, 2, 2);
  381.     if (drawColor)
  382.         RGBForeColor(&pat->cdark);
  383.     else
  384.         PenPat( &pat->black );
  385.     FrameRect(&frame);
  386.  
  387.     OffsetRect(&frame, -1, -1);
  388.     if (drawColor)
  389.         RGBForeColor(&pat->cgray);
  390.     else
  391.         PenPat( &pat->gray );
  392.     FrameRect( &frame );
  393. }
  394.  
  395. drawonce( param, var, window, windowRect, pat, drawColor)
  396.     long        param;
  397.     short        var;
  398.     WindowPtr    window;
  399.     Rect        windowRect;
  400.     patsPtr        pat;
  401.     Boolean        drawColor;
  402. {
  403.     Rect        thisRect;
  404.  
  405.     SICNPtr   goAwaySICNPtr;
  406.     SICN       goAwaySICN;
  407.     SICN           zoomSICN;
  408.     
  409.     goAwaySICN[0] = 0xFFFF;
  410.     goAwaySICN[1] = 0x8002;
  411.     goAwaySICN[2] = 0x9557;
  412.     goAwaySICN[3] = 0xAAAA;
  413.     goAwaySICN[4] = 0x9557;
  414.     goAwaySICN[5] = 0xAAAA;
  415.     goAwaySICN[6] = 0x900F;
  416.     goAwaySICN[7] = 0xA55A;
  417.     
  418.     goAwaySICN[8] = 0x92AF;
  419.     goAwaySICN[9] = 0xA55A;
  420.     goAwaySICN[10] = 0x92AF;
  421.     goAwaySICN[11] = 0xB7FA;
  422.     goAwaySICN[12] = 0x9FFF;
  423.     goAwaySICN[13] = 0xAAAA;
  424.     goAwaySICN[14] = 0xFFFF;
  425.     goAwaySICN[15] = 0xAAAA;
  426. /*
  427. goAwaySICNPtr = (SICNPtr)&"0xFFFF8002AAAB9556"
  428.                             "A00B9556A2BB9556"
  429.                             "A2BB9556A2BB97F6"
  430.                             "AAAB9556FFFFAAAA";
  431.     
  432. goAwaySICNPtr = (SICNPtr)&"0xFFFE80039556AAAB9556AAAB900EA55B92AEA55B92AEB7FB9FFEAAABFFFE5555";
  433.  
  434.                             
  435. goAwaySICNPtr = &goAwaySICN;
  436. goAwaySICNPtr = (SICNPtr)&("0xFFFE80039556AAAB9556AAAB900EA55B92AEA55B92AEB7FB9FFEAAABFFFE5555");
  437. */
  438.     zoomSICN[0] = 0xFFFF;
  439.     zoomSICN[1] = 0x8002;
  440.     zoomSICN[2] = 0xAAAB;
  441.     zoomSICN[3] = 0x9556;
  442.     zoomSICN[4] = 0xA00B;
  443.     zoomSICN[5] = 0x9556;
  444.     zoomSICN[6] = 0xA2BB;
  445.     zoomSICN[7] = 0x9556;
  446.     
  447.     zoomSICN[8] = 0xA2BB;
  448.     zoomSICN[9] = 0x9556;
  449.     zoomSICN[10] = 0xA2BB;
  450.     zoomSICN[11] = 0x97F6;
  451.     zoomSICN[12] = 0xAAAB;
  452.     zoomSICN[13] = 0x9556;
  453.     zoomSICN[14] = 0xFFFF;
  454.     zoomSICN[15] = 0xAAAA;
  455.  
  456.     switch ( (short)param ) {
  457.     case ( 0 ):
  458.         
  459.         thisRect = windowRect;
  460.  
  461.         PenNormal();
  462.         
  463.         /* ——————————————————————————————————————— now for the more specific parts */
  464.         switch ( var ) {
  465.         case ( documentProc ):                    /* a regular window */
  466.         case ( documentProc + 5):                /* a non-modal dialog */
  467.         case ( documentProc + 8):                /* a zooming window */
  468.         case ( documentProc + 12):                /* a zooming window with no grow box */
  469.         case ( documentProc + 13):                /* a zooming non-modal dialog */
  470.         case ( noGrowDocProc ):                    /* a regular window with no grow box */
  471.             DrawFrame(pat, thisRect, 2, drawColor);
  472.             InsetRect(&thisRect, 1,1);
  473.             
  474.             thisRect.bottom = thisRect.top + 19;
  475.             PenPat(&pat->black);
  476.             if (drawColor)
  477.                 RGBForeColor(&pat->cgray);
  478.             else
  479.                 PenPat( &pat->gray );
  480.             PaintRect( &thisRect );
  481.             PenPat( &pat->black );
  482.             if (drawColor)
  483.                 RGBForeColor(&pat->cdark);
  484.             PenSize( 1, 1 );
  485.             MoveTo( windowRect.left+3, windowRect.top+19 );
  486.             LineTo( windowRect.right-4, windowRect.top+19 );
  487.  
  488.             if ( (*(WindowPeek)window).hilited ) {
  489.                 /* ——————————————————————————————— the goaway box */
  490.                 if ( (*(WindowPeek)window).goAwayFlag ) {
  491.                     SetRect( &thisRect, 
  492.                         windowRect.left     + 3, 
  493.                         windowRect.top         + 3,
  494.                         windowRect.left     + 19,
  495.                         windowRect.top         + 19
  496.                     );
  497.                     DrawPane(pat, thisRect, false, drawColor);
  498.                     /* add the symbol */
  499.                     /* ToggleGoaway(thisRect, pat, false, drawColor);  */
  500.                     PlotSICN(&thisRect, &goAwaySICN);
  501.                 }
  502.                 /* ——————————————————————————————— the zoom box */
  503.                 if ( (*(WindowPeek)window).spareFlag ) {
  504.                     SetRect( &thisRect, 
  505.                         windowRect.right     - 18, 
  506.                         windowRect.top         + 2,
  507.                         windowRect.right     - 2,
  508.                         windowRect.top         + 18
  509.                     );
  510.                     DrawPane(pat, thisRect, false, drawColor);
  511.                     /* add the symbol */
  512.                     /* ToggleZoom(thisRect, pat, false, drawColor); */
  513.                     PlotSICN(&thisRect, &zoomSICN);
  514.  
  515.                 }
  516.                 /* ——————————————————————————————— the title */
  517.                 SetRect( &thisRect, 
  518.                     windowRect.left     + 20, 
  519.                     windowRect.top         + 3,
  520.                     windowRect.right     - 19,
  521.                     windowRect.top         + 17
  522.                 );
  523.                 PenSize( 1, 1 );
  524.                 if ( thisRect.right - thisRect.left > 0 )
  525.                     drawWindowTitle( window, &thisRect, pat, true, drawColor );
  526.             } else {
  527.                 /* ——————————————————————————————— the blank title bar */
  528.                 if (!drawColor) {
  529.                     thisRect.top += 2;
  530.                     thisRect.left += 2;
  531.                     thisRect.bottom = thisRect.top + 14;
  532.                     thisRect.right -=2;
  533.                     DrawPane(pat, thisRect, false, drawColor);
  534.                 }
  535.                 /* ——————————————————————————————— the title */
  536.                 SetRect( &thisRect, 
  537.                     windowRect.left     + 20, 
  538.                     windowRect.top         + 3,
  539.                     windowRect.right     - 19,
  540.                     windowRect.top         + 17
  541.                 );
  542.                 drawWindowTitle( window, &thisRect, pat, false, drawColor );
  543.             }
  544.             break;
  545.         
  546.         case ( dBoxProc ):                        /* dialog box variation */
  547.             DrawFrame(pat, thisRect, 4, drawColor);
  548.             thisRect.right -= 2;
  549.             thisRect.bottom -= 2;
  550.             OffsetRect(&thisRect, 1, 1);
  551.             InsetRect( &thisRect, 5, 5 );
  552.             PenSize( 2, 2 );
  553.             if (drawColor) {
  554.                 PenPat(&pat->black);
  555.                 RGBForeColor(&pat->clight);
  556.             } else
  557.                 PenPat( &pat->white );
  558.             FrameRect( &thisRect );
  559.             break;
  560.         case ( plainDBox ):                        /* plain box variation */
  561.             FrameRect( &thisRect );
  562.             break;
  563.         case ( altDBoxProc ):                    /* shadow box variation */
  564.             thisRect.right -= 2;
  565.             thisRect.bottom -= 2;
  566.             FrameRect( &thisRect );
  567.             PenSize( 2, 2 );
  568.             if (drawColor)
  569.                 RGBForeColor(&pat->cgray);
  570.             MoveTo( thisRect.left + 2, thisRect.bottom );
  571.             LineTo( thisRect.right, thisRect.bottom );
  572.             LineTo( thisRect.right, thisRect.top + 2 );
  573.             break;
  574.         }
  575.         
  576.         break;
  577.         
  578.     case ( wInGoAway ):
  579.     
  580.         SetRect( &thisRect,                         /* just toggle the goAway */
  581.             windowRect.left     + 3, 
  582.             windowRect.top         + 3,
  583.             windowRect.left     + 17,
  584.             windowRect.top         + 17
  585.         );
  586.         buttonState = !buttonState;
  587.         if (drawColor)
  588.             ToggleGoaway(thisRect, pat, buttonState, drawColor);
  589.         else
  590.             InvertRect(&thisRect);
  591.         
  592.         break;
  593.         
  594.     case ( wInZoomIn ):
  595.     case ( wInZoomOut ):
  596.  
  597.         SetRect( &thisRect,                         /* just toggle the zoomBox */
  598.             windowRect.right     - 17, 
  599.             windowRect.top         + 3,
  600.             windowRect.right     - 3,
  601.             windowRect.top         + 17
  602.         );
  603.         buttonState = !buttonState;
  604.         if (drawColor)
  605.             ToggleZoom(thisRect, pat, buttonState, drawColor);
  606.         else
  607.             InvertRect(&thisRect);
  608.         
  609.         break;
  610.         
  611.     default:
  612.         break;
  613.         
  614.     }
  615. }
  616.